﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PortalCurriculo.WebUI.UserControls.Basico
{
    public partial class TextBoxTexto : UserControl
    {
        public string Value
        {
            get { return TextBox.Text; }
            set { TextBox.Text = value; }
        }

        public string LabelText
        {
            get { return Label.Text; }
            set
            {
                Label.Visible = true;
                Label.Text = value;
            }
        }

        public bool Obrigatorio
        {
            get { return rfvTextBox.Visible; }
            set { rfvTextBox.Visible = value; }
        }

        public int MaxLength { private get; set; }
        public Unit Width { set { Panel.Width = value; } }
        public string ValidationExpression { private get; set; }
        public TextBoxMode TextMode { private get; set; }
        public int Columns { private get; set; }
        public int Rows { private get; set; }
        public string OnKeyUp { private get; set; }

        public ServerValidateEventHandler ServerValidation
        {
            set { ctvTextBox.ServerValidate += value; }
        }

        public string ClientValidationFunction
        {
            set
            {
                ctvTextBox.Visible = true;
                ctvTextBox.ClientValidationFunction += value;
            }
            get { return ctvTextBox.ClientValidationFunction; }
        }

        public bool CustomValidateEmptyText
        {
            set
            {
                ctvTextBox.ValidateEmptyText = value;
                ctvTextBox.Visible = true;
            }
            get
            {
                return ctvTextBox.ValidateEmptyText;
            }
        }

        public event ServerValidateEventHandler OnServerValidation
        {
            add
            {
                ctvTextBox.ServerValidate += value;
                ctvTextBox.Visible = true;
            }
            remove
            {
                value = null;
                ctvTextBox.Visible = false;
            }
        }

        public string ValidationGroup
        {
            get { return TextBox.ValidationGroup; }
            set { TextBox.ValidationGroup = rfvTextBox.ValidationGroup = revTextBox.ValidationGroup = ctvTextBox.ValidationGroup = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rfvTextBox.SetFocusOnError = true;
            revTextBox.Visible = !string.IsNullOrEmpty(ValidationExpression);
            TextBox.MaxLength = MaxLength;
            if (OnKeyUp != null) TextBox.Attributes.Add("onkeyup", OnKeyUp);
            TextBox.TextMode = TextMode;

            if (TextMode != TextBoxMode.MultiLine) return;

            TextBox.Columns = Columns;
            TextBox.Rows = Rows;
            ctvTextBox.ServerValidate += Validar;
        }

        protected void Validar(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            ctvTextBox.Visible = true;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Label.AssociatedControlID = TextBox.ID;
            base.Render(writer);
        }
    }
}
